package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private RecommendUserApi recommendUserApi;

    //相互喜欢
    @Override
    public long queryEachLoveCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long eachLoveCount = mongoTemplate.count(query, Friend.class);
        return eachLoveCount;
    }


    //我喜欢
    @Override
    public long queryLoveCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long loveCount = mongoTemplate.count(query, UserLike.class);
        return loveCount;
    }

    //粉丝数
    @Override
    public long queryFansCount(long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long fansCount = mongoTemplate.count(query, UserLike.class);
        return fansCount;
    }

    //朋友
    @Override
    public PageResult queryEachLove(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page - 1) * pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, Friend.class);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (friendList != null) {
            for (Friend friend : friendList) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", friend.getFriendId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, friend.getFriendId());
                map.put("score", score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page, pagesize, (int) counts, mapList);
        return pageResult;
    }


    //我喜欢
    @Override
    public PageResult queryLove(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page - 1) * pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (userLikeList != null) {
            for (UserLike userLike : userLikeList) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", userLike.getLikeUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, userLike.getLikeUserId());
                map.put("score", score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page, pagesize, (int) counts, mapList);
        return pageResult;
    }

    //我的粉丝
    @Override
    public PageResult queryFans(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.skip((page - 1) * pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (userLikeList != null) {
            for (UserLike userLike : userLikeList) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", userLike.getUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, userLike.getUserId());
                map.put("score", score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page, pagesize, (int) counts, mapList);
        return pageResult;
    }


    //我的访客
    @Override
    public PageResult queryVisitors(Integer page, Integer pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.skip((page - 1) * pagesize).limit(pagesize);

        long counts = mongoTemplate.count(query, Visitors.class);
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);

        //遍历所有的朋友,把朋友的id与与朋友的缘分值添加到Map中
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (visitorsList != null) {
            for (Visitors visitors : visitorsList) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", visitors.getVisitorUserId());
                //查询我和朋友的缘分值
                Long score = recommendUserApi.queryScore(userId, visitors.getVisitorUserId());
                map.put("score", score.intValue());
                mapList.add(map);
            }
        }

        PageResult pageResult = new PageResult(page, pagesize, (int) counts, mapList);
        return pageResult;
    }

    //删除粉丝的数据
    @Override
    public void delete(long userId, Long fansId) {
        Query query = new Query(Criteria.where("userId").is(fansId).and("likeUserId").is(userId));
        mongoTemplate.remove(query, UserLike.class);
    }

    @Override
    public void addLikeUser(UserLike userLike) {

        mongoTemplate.save(userLike);
    }

    @Override
    public List<UserLike> findLikeUser(long userId, String likeId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeId));

        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public void checkAndAddFriend(long userId, String likeId) {
        //检查双方是否互相喜欢
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeId));
        Query query1 = new Query(Criteria.where("userId").is(likeId).and("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<UserLike> userLikes1 = mongoTemplate.find(query1, UserLike.class);
        if (userLikes1.size() > 0 && userLikes.size() > 0) {
            //移除喜欢关系
            mongoTemplate.remove(query, UserLike.class);
            mongoTemplate.remove(query1, UserLike.class);
            //添加好友关系
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(Long.valueOf(likeId));
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
            //保存一个好友方 好友关系到 tanhua_users
            friend.setUserId(Long.valueOf(likeId));
            friend.setFriendId(userId);
            mongoTemplate.save(friend);
        }
    }

    /**
     * 添加一条 粉丝喜欢记录
     */
    @Override
    public void addFanLike(UserLike userLike) {
        mongoTemplate.insert(userLike);
    }
    /**
     * 移除一条用户喜欢别人记录
     */
    @Override
    public void deleteLikeUser(Long userId, Long fansId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(fansId));
        mongoTemplate.remove(query, UserLike.class);
    }
}
